<template>
  <div :class="['modal', {fade: fade}]" data-backdrop="static" data-disable="false" data-keyboard="true" tabindex="-1">
    <div :class="['modal-dialog', size]">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title text-center text-primary"><span>{{ title }}</span></h4>
        </div>
        <div class="modal-body">
          <form class="form-inline" autocomplete="off" spellcheck="false">
            <div class="form-group form-group-sm">
              <label>搜索</label>
              <input type="text" class="form-control" placeholder="关键字" v-model.trim="q" @keydown.enter.prevent ref="q">
            </div>
            <span class="hidden-xs">&nbsp;&nbsp;</span>
            <div class="form-group form-group-sm">
              <label>通道类型</label>
              <select class="form-control" v-model.trim="channel_type">
                <option value="">全部</option>
                <option value="device">设备</option>
                <option value="dir">子目录</option>
              </select>
            </div>
            <span class="hidden-xs">&nbsp;&nbsp;</span>
            <div class="form-group form-group-sm">
              <div class="checkbox">
                <el-checkbox style="margin-top:-5px;padding-left:0;" size="small" v-model.trim="related" name="Related">
                  只看已选({{ relateCnt }})
                </el-checkbox>
              </div>
            </div>
          </form>
          <br>
          <el-table :data="channels" stripe @sort-change="sortChange" @select="select" @select-all="selectAll"
                    :max-height="500"
                    ref="channelTable" v-loading="loading" element-loading-text="加载中...">
            <el-table-column type="selection" width="55" fixed :selectable="selectable"></el-table-column>
            <el-table-column prop="DeviceID" label="设备国标编号" min-width="200" show-overflow-tooltip
                             sortable="custom"></el-table-column>
            <el-table-column prop="ID" label="通道国标编号" min-width="200" show-overflow-tooltip
                             sortable="custom"></el-table-column>
            <el-table-column prop="Name" label="通道名称" min-width="120" :formatter="formatName"
                             show-overflow-tooltip></el-table-column>
            <!-- <el-table-column prop="Status" label="在线状态" min-width="100">
                <template slot-scope="props">
                <span v-if="props.row.SubCount > 0">-</span>
                <span v-else-if="props.row.Status == 'ON'" class="text-success">在线</span>
                <span v-else>离线</span>
                </template>
            </el-table-column> -->
            <!-- <el-table-column prop="SubCount" label="子节点数" min-width="100" sortable="custom"></el-table-column> -->
            <el-table-column prop="Manufacturer" label="厂家" min-width="120" :formatter="formatManufacturer"
                             show-overflow-tooltip></el-table-column>
          </el-table>
          <el-pagination v-if="total > 0" layout="total,prev,pager,next,sizes" :pager-count="isMobile() ? 3 : 5"
                         class="pull-right" :total="total" :page-size.sync="pageSize"
                         :current-page.sync="currentPage"></el-pagination>
          <div class="clearfix"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import 'jquery-ui/ui/widgets/draggable'
import $ from 'jquery'
import _ from "lodash";

export default {
  props: {
    title: {
      default: ''
    },
    size: {
      type: String,
      default: ''
    },
    fade: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      q: "",
      channel_type: "",
      total: 0,
      relateCnt: 0,
      pageSize: 10,
      currentPage: 1,
      sort: "",
      order: "",
      related: false,
      loading: false,
      channels: [],
      selection: [],
      bak: {}, // serial:code <-> custom
      pcode: '', //外部关联 code
    }
  },
  watch: {
    q: function (newVal, oldVal) {
      this.doDelaySearch();
    },
    channel_type: function (newVal, oldVal) {
      this.doSearch();
    },
    related: function (newVal, oldVal) {
      this.doSearch();
    },
    currentPage: function (newVal, oldVal) {
      this.doSearch(newVal);
    },
    pageSize: function (newVal, oldVal) {
      this.doSearch();
    }
  },
  mounted() {
    $(this.$el).find('.modal-content').draggable({
      handle: '.modal-header',
      cancel: ".modal-title span",
      addClasses: false,
      containment: 'document',
      delay: 100,
      opacity: 0.5
    });
    $(this.$el).on("shown.bs.modal", () => {
      this.$emit("show");
    }).on("hidden.bs.modal", () => {
      this.errors.clear();
      this.reset();
      this.$emit("hide");
    })
  },
  methods: {
    sortChange(data) {
      this.sort = data.prop;
      this.order = data.order == "ascending" ? "asc" : "desc";
      this.getChannels();
    },
    select(selection, row) {
      var idx = selection.indexOf(row);
      if (idx >= 0) {
        $.get(this.$baseUrl.http + "/api/v1/channel/setcustomparent", {
          customs: [`${row.DeviceID}:${row.ID}:${this.pcode}`]
        }).then(() => {
          this.bak[`${row.DeviceID}:${row.ID}`] = row.CustomParentID || "";
        }).always(() => {
          this.getChannels();
        })
      } else {
        var bakCustom = this.bak[`${row.DeviceID}:${row.ID}`] || "";
        $.get(this.$baseUrl.http + "/api/v1/channel/setcustomparent", {
          customs: [`${row.DeviceID}:${row.ID}:${bakCustom}`]
        }).always(() => {
          this.getChannels();
        })
      }
    },
    selectAll(selection) {
      var keys = [];
      var baks = {};
      if (selection.length) {
        for (var row of selection) {
          var idx = this.selection.indexOf(row);
          if (idx < 0) {
            keys.push(`${row.DeviceID}:${row.ID}:${this.pcode}`)
            baks[`${row.DeviceID}:${row.ID}`] = row.CustomParentID || "";
          }
        }
        $.get(this.$baseUrl.http + "/api/v1/channel/setcustomparent", {
          customs: keys,
        }).then(() => {
          this.bak = Object.assign(this.bak, baks);
        }).always(() => {
          this.getChannels();
        })
      } else {
        for (var row of this.selection) {
          var bakCustom = this.bak[`${row.DeviceID}:${row.ID}`] || "";
          keys.push(`${row.DeviceID}:${row.ID}:${bakCustom}`)
        }
        $.get(this.$baseUrl.http + "/api/v1/channel/setcustomparent", {
          customs: keys,
        }).always(() => {
          this.getChannels();
        })
      }
    },
    doSearch(page = 1) {
      this.currentPage = page;
      this.getChannels();
    },
    doDelaySearch: _.debounce(function () {
      this.doSearch();
    }, 500),
    formatName(row, col, cell) {
      return row.CustomName || row.Name || "-";
    },
    formatManufacturer(row, col, cell) {
      if (cell) return cell;
      return "-";
    },
    selectable(row, index) {
      return true;
    },
    getChannels() {
      if (!this.pcode) return;
      this.loading = true;
      $.get(this.$baseUrl.http + "/api/v1/channel/customlist", {
        pcode: this.pcode,
        q: this.q,
        start: (this.currentPage - 1) * this.pageSize,
        limit: this.pageSize,
        channel_type: this.channel_type,
        related: this.related,
        sort: this.sort,
        order: this.order
      }).then(ret => {
        this.$refs["channelTable"].clearSelection();
        this.total = ret.ChannelCount;
        this.relateCnt = ret.ChannelRelateCount;
        this.channels = ret.ChannelList || [];
        this.selection = [];
        this.$nextTick(() => {
          this.channels.forEach(row => {
            var sel = row.CustomParentID == this.pcode;
            this.$refs["channelTable"].toggleRowSelection(row, sel);
            if (sel) {
              this.selection.push(row);
            }
          })
        })
      }).always(() => {
        this.loading = false;
      });
    },
    reset() {
      this.pcode = '';
      this.$refs["channelTable"].clearSelection();
      this.channels = [];
      this.selection = [];
      this.bak = {};
      this.q = "";
      this.channel_type = "";
      this.related = false;
      this.currentPage = 1;
      this.pageSize = 10;
    },
    show(pcode) {
      this.pcode = pcode;
      $(this.$el).modal("show");
      this.getChannels();
    },
    hide() {
      $(this.$el).modal("hide");
    }
  }
}
</script>

<style lang="less" scoped>
label.el-switch {
  margin-bottom: -10px;
}
</style>



